{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "简单收益率 = (票面利率×债券面值) / 债券的购买价格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "债券的简单收益率为: 5.26%\n"
     ]
    }
   ],
   "source": [
    "def calculate_simple_yield(face_value, coupon_rate, purchase_price):\n",
    "    \"\"\"\n",
    "    计算债券的简单收益率\n",
    "    \n",
    "    参数：\n",
    "    - face_value: 债券的面值（如1000元）\n",
    "    - coupon_rate: 债券的票面利率（如5% 为 0.05）\n",
    "    - purchase_price: 债券的购买价格（如950元）\n",
    "    \n",
    "    返回：\n",
    "    - 简单收益率\n",
    "    \"\"\"\n",
    "    # 计算年票息收入\n",
    "    annual_coupon = face_value * coupon_rate\n",
    "    \n",
    "    # 计算简单收益率\n",
    "    simple_yield = annual_coupon / purchase_price\n",
    "    \n",
    "    return simple_yield\n",
    "\n",
    "# 示例：债券面值1000元，票面利率5%，购买价格950元\n",
    "face_value = 1000  # 债券面值\n",
    "coupon_rate = 0.05  # 票面利率\n",
    "purchase_price = 950  # 购买价格\n",
    "\n",
    "simple_yield = calculate_simple_yield(face_value, coupon_rate, purchase_price)\n",
    "print(f\"债券的简单收益率为: {simple_yield * 100:.2f}%\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当期收益率 = 票面利率×债券面值 / 债券当前市场价格\n",
    "\n",
    "衡量债券当前的收益水平\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "债券的当期收益率为: 5.26%\n"
     ]
    }
   ],
   "source": [
    "def calculate_current_yield(face_value, coupon_rate, current_price):\n",
    "    \"\"\"\n",
    "    计算债券的当期收益率\n",
    "    \n",
    "    参数：\n",
    "    - face_value: 债券的面值（如1000元）\n",
    "    - coupon_rate: 债券的票面利率（如5% 为 0.05）\n",
    "    - current_price: 债券的当前市场价格（如950元）\n",
    "    \n",
    "    返回：\n",
    "    - 当期收益率\n",
    "    \"\"\"\n",
    "    # 计算年票息收入\n",
    "    annual_coupon = face_value * coupon_rate\n",
    "    \n",
    "    # 计算当期收益率\n",
    "    current_yield = annual_coupon / current_price\n",
    "    \n",
    "    return current_yield\n",
    "\n",
    "# 示例：债券面值1000元，票面利率5%，当前市场价格950元\n",
    "face_value = 1000  # 债券面值\n",
    "coupon_rate = 0.05  # 票面利率\n",
    "current_price = 950  # 当前市场价格\n",
    "\n",
    "current_yield = calculate_current_yield(face_value, coupon_rate, current_price)\n",
    "print(f\"债券的当期收益率为: {current_yield * 100:.2f}%\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "期收益率（Yield to Maturity, YTM） 是一个综合性的债券收益率指标，表示一个债券持有至到期时，投资者获得的年化回报率，假设债券的现金流按预定计划支付并且利息能够再投资。YTM 考虑了债券的当前价格、面值、票息支付及剩余期限。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "债券的到期收益率（YTM）为: 1.69%\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy.optimize import fsolve\n",
    "\n",
    "def calculate_ytm(face_value, coupon_rate, current_price, years, payment_frequency=1):\n",
    "    \"\"\"\n",
    "    计算债券的到期收益率（YTM），支持小数时间\n",
    "\n",
    "    参数：\n",
    "    - face_value: 债券的面值（例如1000元）\n",
    "    - coupon_rate: 债券的票面利率（例如5% 为 0.05）\n",
    "    - current_price: 债券的当前市场价格（例如950元）\n",
    "    - years: 债券的到期年限（例如 10年）\n",
    "    - payment_frequency: 每年支付利息的次数，默认每年一次，支持每年支付多次利息（如半年付息）\n",
    "\n",
    "    返回：\n",
    "    - 到期收益率（YTM）\n",
    "    \"\"\"\n",
    "    # 票息收入，每期支付的利息\n",
    "    coupon_payment = face_value * coupon_rate / payment_frequency\n",
    "    \n",
    "    # 定义 YTM 的方程，利用 fsolve 求解\n",
    "    def ytm_equation(ytm):\n",
    "        price = 0\n",
    "        # 迭代每个时间点，考虑每期利息支付的时间间隔\n",
    "        for t in range(1, years * payment_frequency + 1):\n",
    "            price += coupon_payment / (1 + ytm / payment_frequency) ** t  # 支付时间是t，利率是每期的利率\n",
    "        price += face_value / (1 + ytm / payment_frequency) ** (years * payment_frequency)  # 最后一期的面值\n",
    "        return price - current_price  # 目标是让债券的现值等于当前市场价格\n",
    "    \n",
    "    # 初始猜测为 5%\n",
    "    ytm = fsolve(ytm_equation, 0.05)[0]\n",
    "    \n",
    "    return ytm\n",
    "\n",
    "# 示例：债券面值100元，票面利率2.3%，当前市场价格104.5603元，到期年限10年\n",
    "face_value = 100  # 债券面值\n",
    "coupon_rate = 0.023  # 票面利率\n",
    "current_price = 105.5603  # 当前市场价格\n",
    "years = 10  # 到期年限\n",
    "payment_frequency = 2  # 假设每年支付两次利息（半年付息）\n",
    "\n",
    "ytm = calculate_ytm(face_value, coupon_rate, current_price, years, payment_frequency)\n",
    "print(f\"债券的到期收益率（YTM）为: {ytm * 100:.2f}%\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
